package com.itboye.sunsun.person.adapter;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.List;

import com.itboye.sunsun.adapter.AbstractOrderAdapter;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.beans.OrderQueRenDingDan;
import com.itboye.sunsun.beans.OrderBean.InnerDetailBean;
import com.itboye.sunsun.buy.ui.QueRenZhiFuActivity;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsun.utils.Sha1Md5Util;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;
import com.itboye.sunsun.volley.MyJsonRequest.Builder;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.Toast;

public class PayOrderAdapter extends AbstractOrderAdapter {

	public PayOrderAdapter(Context context, List<InnerDetailBean> list) {
		super(context, list);
	}

	@Override
	public String onGetLeftTextviewText() {
		return "取消订单";
	}

	@Override
	public String onGetRightTextviewText() {
		return "付款";
	}

	@Override
	public void onLeftViewClick(View v) {
		deleteorder((String)v.getTag());
	} 

	@Override
	public void onRightViewClick(View v) {
		
		tijiaoDingdan((String)v.getTag());
		

	}

	
	private void deleteorder(final String s){
		MyJsonRequest<String>request=new MyJsonRequest.Builder<String>()
				.typeKey("BY_Orders_qxorder")
				.apiVer("100")
				.param("order_code", s)
				.param("uid", (String)SPUtils.get(context, null, SPContants.USER_ID	, ""))
				.requestListener(new XRequestListener<String>() {
					@Override
					public void onResponse(String arg0) {
						 
						//Toast.makeText(context, arg0, 1).show();
						Iterator<InnerDetailBean> itr=list.iterator();
						while ( itr.hasNext()) {
							if (s.equals(itr.next().getOrderCode())) {
								itr.remove();
							}
						}
						
						notifyDataSetChanged();
					}
				})
				.errorListener(new XErrorListener() {
					
					@Override
					public void onErrorResponse(Exception exception, int code, String msg) {
						 
						
					}
				})
				.build();
		
		HttpRequest.getDefaultRequestQueue().add(request);
	}
	
	//支付订单
	private void tijiaoDingdan(String  orderCode) {
		Builder<OrderQueRenDingDan>builder=new MyJsonRequest.Builder<OrderQueRenDingDan>()
				.apiVer("100")
				.typeKey("BY_Orders_pay")
				.param("uid", SPUtils.get(App.ctx, null, SPContants.USER_ID, ""))
				 .param("order_id", orderCode)

				
				.requestListener(new XRequestListener<OrderQueRenDingDan>() {
					
					@Override
					public void onResponse(OrderQueRenDingDan arg0) {
						 
						String name=arg0.getName();
						String desc=arg0.getDesc();
						
						name=URLDecoder.decode(name);
						desc=URLDecoder.decode(desc);
						
						
						String str=arg0.getCode()+name+desc+arg0.getTotalPrice();
						
						str=URLEncoder.encode(str);
						
						str=str.toLowerCase();
						
						 
						
						str=Sha1Md5Util.SHA1(Sha1Md5Util.MD5(str));
						
						str=str+"ITBOYE_PAY_DASFSAFKL";
						
						str=Sha1Md5Util.MD5(str);
						
						if (arg0.getKey().equals(str)) {
							Intent intent=new Intent(App.ctx, QueRenZhiFuActivity.class);
							intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
							intent.putExtra("what", arg0);
							App.ctx.startActivity(intent);
						}else{
							Toast.makeText(App.ctx, "订单异常", 1).show();
						}
						notifyDataSetChanged();
						
						// DebugToast.show("付款。。。", 1);
						
					}
				})
				.errorListener(new XErrorListener() {
					
					@Override
					public void onErrorResponse(Exception exception, int code, String msg) {
						
					}
				});
		
//		if (youhuiquanId!=null) {
//			builder.param("idcode", youhuiquanId);
//		}
//		
		HttpRequest.getDefaultRequestQueue().add(builder.build());
		
	}
	
}
